<template>
	<view class="myCollectionPage">
		<view class="tabContent">
			<view @click="changeTab(item.id)" v-for="(item,index) in tabs" :key="index" class="tabItem"
				:class="activeTab===item.id?'isActive':''">{{item.name}}</view>
		</view>
		<view class="newsDetial" v-if="isLoad">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-if="!list.length" text="没有帖子">
			</u-empty>
			<view class="solutionList" v-for="(item,index) in list" :key="index" @click="goDetail(item)">
				<view class="itemTop">
					<view class="topRight">
						<u--image :src="item.images?($appFunc.getFileUrl(item.images.split(',')[0])):''" shape="square"
							mode="aspectFill" width="208rpx" height="150rpx" bgColor="transparent" radius="8rpx">
						</u--image>
					</view>
					<view class="topLeft">
						<view class="title">{{item.name}}</view>
						<view class="talk">
							{{item.createTime}}
						</view>
					</view>
				</view>
				<view class="itemBottom">
					<view class="operate">
						<view class="item">
							<u--image src="/static/community/4.png" shape="square" mode="aspectFill" width="30rpx"
								height="24rpx"></u--image>
							<view class="count">{{item.readingVolume||0}}</view>
						</view>
						<view class="item">
							<u--image src="/static/community/5.png" shape="square" mode="aspectFill" width="26rpx"
								height="26rpx"></u--image>
							<view class="count">{{item.replyCount||0}}</view>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore v-if="list.length" :status="isFinish?'nomore':'loading'" loading-text="正在加载"
				loadmore-text="加载更多" nomore-text="没有更多了" color="#d0d0d0" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				api: this.$api.baseUrl,
				list: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				userId: "",
				isFinish: false,
				isLoading: false,
				isLoad: false,
				activeTab: "1",
				tabs: [{
					name: "产品解决方案",
					id: "1"
				}, {
					name: "新闻资讯",
					id: "2"
				}, {
					name: "帖子",
					id: "3"
				}]
			}
		},
		computed: {
			getReleaseTime(date) {
				return function(date) {
					if (date) {
						if (typeof date === 'number') {
							return new Date(date).format("YYYY-MM-dd hh:mm")
						} else {
							return new Date(date.replace(/\-/g, "/")).format("YYYY-MM-dd hh:mm")
						}
					} else {
						return ""
					}
				}
			},
		},
		onReachBottom() {
			if (!this.isLoading && !this.isFinish) {
				this.pageNo++;
				this.initData();
			}
		},
		onPullDownRefresh() {
			this.doRefresh();
		},

		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.initData()
				}
			})
		},
		methods: {
			doRefresh() {
				this.isFinish = false;
				this.isLoad = false;
				if (!this.isLoading) {
					this.pageNo = 1;
					this.list = [];
					this.initData(true);
				}
			},
			changeTab(id) {
				this.activeTab = id;
				this.doRefresh();
			},
			initData(isRefresh) {
				this.isLoading = true;
				Promise.all([
					this.$request.get(this.$api.myStar, {
						userId: this.userId,
						type: this.activeTab,
						pageNo: this.pageNo,
						pageSize: this.pageSize
					}),
				]).then(res => {
					if (isRefresh) {
						uni.stopPullDownRefresh()
					}
					uni.hideLoading()
					this.isLoading = false;
					this.list = this.list.concat(res[0].result.records)
					this.total = res[0].result.total;
					if (this.pageNo * this.pageSize < this.total) {
						this.isFinish = false
					} else {
						this.isFinish = true
					}
					this.isLoad = true;
				})
			},
			goDetail(item) {
				if (this.activeTab === "1") {
					uni.navigateTo({
						url: "/pages/solutionDetial/solutionDetial?id=" + item.id,
					})
				} else if (this.activeTab === "2") {
					uni.navigateTo({
						url: "/pages/newsDetial/newsDetial?id=" + item.id,
					})
				} else if (this.activeTab === "3") {
					uni.navigateTo({
						url: "/pages/postDetail/postDetail?id=" + item.id,
					})
				}else if (this.activeTab === "4") {
					
				}

			}
		}
	}
</script>

<style lang="scss">
	.myCollectionPage {
		padding: 0 40rpx 40rpx 40rpx;

		.tabContent {
			display: flex;
			flex-wrap: nowrap;
			justify-content: flex-start;
			width: calc(100vw - 80rpx);
			overflow: auto;
			padding: 20rpx 0;

			.tabItem {
				background: #F4F4F4;
				border-radius: 8rpx;
				color: #333333FF;
				font-size: 26rpx;
				height: 52rpx;
				padding: 0 40rpx;
				line-height: 52rpx;
				margin-right: 20rpx;
				flex-shrink: 0;
			}

			.isActive {
				color: #004DA1FF;
				background-color: #E5EDFFFF;
			}
		}

		.solutionList {
			display: flex;
			flex-direction: column;
			padding: 30rpx 20rpx 24rpx;
			background: #FFFFFF;
			box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
			border-radius: 10px 10px 10px 10px;
			margin-bottom: 20rpx;

			.itemTop {
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				padding-bottom: 20rpx;

				.topLeft {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					width: 100%;

					.title {
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						font-size: 28rpx;
						letter-spacing: 1rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 3;
						-webkit-box-orient: vertical;
						padding-right: 20rpx;
					}

					.talk {
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
						font-size: 26rpx;
					}
				}

				.topRight {
					flex-shrink: 0;
					padding-right: 20rpx;
				}
			}

			.itemBottom {
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-between;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				font-size: 26rpx;

				.operate {
					display: flex;
					flex-wrap: nowrap;
					justify-content: flex-start;
					width: 100%;

					.item {
						display: flex;
						flex-wrap: nowrap;
						align-items: center;
						padding-right: 40rpx;

						.count {
							padding-left: 10rpx;
						}
					}
				}
			}
		}
	}
</style>
